﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RPG
{
    public class CGameCamera : CGameObject
    {
        public CGameCamera(GraphicsDeviceManager graphics)
        {
            //Create Projection Matrix
            float ratio = graphics.PreferredBackBufferWidth / graphics.PreferredBackBufferHeight;
            m_Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, ratio, 1, 10000);

            //Create View Matrix
            Position = Vector3.Zero;
            m_TargetPos = Vector3.One;
            if (!CalcViewMatrix())
                throw new Exception("Failed to create View Matrix");
        }

        public override void Update(GameTime gameTime)
        {
            
        }

        protected bool CalcViewMatrix()
        {
            m_View = Matrix.CreateLookAt(Position, m_TargetPos, Vector3.Up);
            if (m_View != null)
                return true;
            return false;
        }

        //Private
        private Matrix m_View;
        private Matrix m_Projection;
        protected Vector3 m_TargetPos;

        //Accessors
        public Matrix ViewMatrix
        {
            get{return m_View;}
            set{m_View = value;}
        }

        //Accessors
        public Matrix ProjectionMatrix
        {
            get { return m_Projection; }
            set { m_Projection = value; }
        }

        public override Vector3 Position
        {
            get{return m_Position;}
            set
            {
                m_Position = value;
                CalcViewMatrix();
            }
        }

        public Vector3 TargetPosition
        {
            get
            {
                return m_TargetPos;
            }
            set
            {
                m_TargetPos = value;
                CalcViewMatrix();
            }
        }
    }
}
